/*
更新日期：2017、01、17
作者：盛彬
备注信息：
SwsContext的C++封装 
*/
#pragma once
#include "AllHeader.hpp"

#ifndef SwsContextPP_H
#define SwsContextPP_H

class SwsContextPP
{
public:
    SwsContextPP();
    ~SwsContextPP();

    int Init(int src_width, int src_height, enum AVPixelFormat src_format, int dst_width, int dst_height, enum AVPixelFormat dst_format);
    int sws_scalePP(const uint8_t *const srcSlice[],
        const int srcStride[], int srcSliceY, int srcSliceH,
        uint8_t *const dst[], const int dstStride[]);
    int sws_scalePP(AVFrame *src_frame, AVFrame *dst_frame);
private:
    SwsContext *swspp{ nullptr };
    bool b_new{ true };
};

inline SwsContextPP::SwsContextPP()
{
}

inline SwsContextPP::~SwsContextPP()
{
    if (b_new)
    {
        return;
    }
    if (swspp != nullptr)
    {
        sws_freeContext(swspp);
    }
}

inline int SwsContextPP::Init(int src_width, int src_height, AVPixelFormat src_format, int dst_width, int dst_height, AVPixelFormat dst_format)
{
    if (!b_new)
    {
        return -1;
    }
    swspp = sws_getContext(src_width, src_height, src_format, dst_width, dst_height, dst_format, SWS_FAST_BILINEAR, NULL, NULL, NULL);
    if (!swspp)
    {
        return -1;
    }
    b_new = false;
    return 0;
}

inline int SwsContextPP::sws_scalePP(const uint8_t *const srcSlice[],
    const int srcStride[], int srcSliceY, int srcSliceH,
    uint8_t *const dst[], const int dstStride[])
{
    if (b_new)
    {
        return -1;
    }
    sws_scale(swspp, srcSlice, srcStride, srcSliceY, srcSliceH, dst, dstStride);//函数返回值意义不大
    return 0;
}

inline int SwsContextPP::sws_scalePP(AVFrame * src_frame, AVFrame * dst_frame)
{
    sws_scale(swspp, src_frame->data, src_frame->linesize, 0, src_frame->height, dst_frame->data, dst_frame->linesize);//函数返回值意义不大
    return 0;
}

#endif // !SwsContextPP_H
